package com.doit.mr.day04.movie;

import com.alibaba.fastjson.JSON;
import com.doit.mr.day04.bean.MovieWritable;
import com.doit.mr.day04.json.Movie;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @DATE 2021/12/9/23:20
 * @Author MDK
 * @Version 2021.2.2
 * 求每部电影的平均分  K:id V:rate
 **/
public class AvgRate {
    static class AvgRateMapper extends Mapper<LongWritable, Text, Text, DoubleWritable>{
        //每行数据执行一次
        Text k = new Text();
        DoubleWritable v = new DoubleWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            try {
                String line = value.toString();
                //将行数据转化成movie 获取id,分数rate并封装起来
                Movie movie = JSON.parseObject(line,Movie.class);
                String uid = movie.getUid();
                double rate = movie.getRate();
                k.set(uid);
                v.set(rate);
                context.write(k,v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //如果最终的输出结果不是电影均分的需求 而是电影的记录 输出的结果没必要K V  所以输出的可以是NullWritable  没有输出内容
    static class AvgRateReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable>{
        DoubleWritable v = new DoubleWritable();

        @Override
        protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
            double sum = 0;
            int cnt = 0;
            for (DoubleWritable value : values) {
                sum += value.get();
                cnt++;
            }
            double avg = sum/cnt;
            v.set(avg);
            context.write(key,v);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "avgRate");
        job.setMapperClass(AvgRateMapper.class);
        job.setReducerClass(AvgRateReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);

        FileInputFormat.setInputPaths(job, new Path("D:\\mrdata\\movie\\input"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\mrdata\\movie\\output_02"));
        job.waitForCompletion(true);
    }
}
